﻿/*
 * @author: wizardc
 */

using System;
using System.IO;
using DouEditor.Utils;
using UnityEditor;
using UnityEngine;

namespace DouEditor.GPU
{
    /// <summary>
    /// GPU 动画导出窗口
    /// </summary>
    public class GPUAnimRecordWindow : EditorWindow
    {
        [MenuItem("DouFramework/GPU/导出GPU动画资源")]
        public static void ShowWindow()
        {
            EditorWindow window = GetWindow<GPUAnimRecordWindow>(false, "导出GPU动画资源");
            window.minSize = new Vector2(550, 400);
        }
        
        private string _exportPath = String.Empty;
        
        private GUIStyle _boxStyle;
        
        private void OnGUI()
        {
            if (_boxStyle == null)
            {
                _boxStyle = new GUIStyle(GUI.skin.box);
                _boxStyle.alignment = TextAnchor.MiddleLeft;
                _boxStyle.fontSize = 13;
                _boxStyle.richText = true;
            }

            using (new GUILayout.VerticalScope())
            {
                GUILayout.Box(new GUIContent("GPU动画导出说明：\n1. 需要选中带有Animator组件的GameObject或预制体；\n2. 会生成新的Mesh和动画贴图以及对应的二进制数据；\n3. 需要自己创建材质关联数据使用；"), _boxStyle, GUILayout.ExpandWidth(true), GUILayout.Height(85));
                using (new GUILayout.HorizontalScope())
                {
                    _exportPath = EditorGUILayout.TextField(new GUIContent("导出文件夹"), _exportPath);
                    if (GUILayout.Button("选择", GUILayout.Width(80)))
                    {
                        var folder = EditorUtility.OpenFolderPanel("选择文件夹", Application.dataPath, "");
                        if (!string.IsNullOrEmpty(folder))
                        {
                            _exportPath = folder;
                        }
                    }
                }
                if (GUILayout.Button("开始导出"))
                {
                    var gos = Selection.gameObjects;
                    if (gos == null || gos.Length == 0)
                    {
                        EditorUtility.DisplayDialog("提示", "没有选择预制体！", "确定");
                        return;
                    }
                    try
                    {
                        AssetDatabase.StartAssetEditing();
                        foreach (var go in gos)
                        {
                            if (!GPUAnimRecord.Record(go, out var animData, out var animTex, out var animMesh))
                            {
                                Debug.LogError($"{AssetDatabase.GetAssetPath(go)}导出GPU动画失败！");
                                continue;
                            }
                            MessagePackUtil.SaveData(Path.Combine(_exportPath, go.name + "_Data.bytes"), animData);
                            AssetDatabase.CreateAsset(animTex, EditorUtil.ConvertToRelativePath(Path.Combine(_exportPath, go.name + "_AnimTex.asset")));
                            AssetDatabase.CreateAsset(animMesh, EditorUtil.ConvertToRelativePath(Path.Combine(_exportPath, go.name + "_Mesh.asset")));
                        }
                        EditorUtility.DisplayDialog("提示", "导出完毕！", "确定");
                    }
                    finally
                    {
                        AssetDatabase.StopAssetEditing();
                        AssetDatabase.Refresh();
                    }
                }
            }
        }
    }
}
